package com.base.myproject.client.HR.recruitment;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * @author cxghiu4102@gmail.com 2010-11-5 职位申请表
 * 
 */
public class PositionRequest extends BaseJDBCPanel {
	String leftTreeDepId = null; // 将当前树形结构中选中的部门id保存

	String rightTreeDepId = null; // 将当前树形结构中选中的部门id保存
	String rightTreeDepName = null; // 将当前树形结构中选中的部门name保存

	TreePanel<TreeNodeModelData> leftTreePanel = null;
	TreePanel<TreeNodeModelData> rightTreePanel = null;

	public PositionRequest() {
		init();
	}

	public void init() {
		initWidget();
		registerMainStore(store);
	}

	public void initWidget() {
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(2);
		VerticalPanel vp = new VerticalPanel();
		vp.add(this.getGrid());
		
		ContentPanel leftPanel = new ContentPanel();
		leftPanel.add(getTreePanel());
		leftPanel.setHeading("选择职位");
		ContentPanel rightPanel = new ContentPanel();
		rightPanel.add(getRightTreePanel());
		rightPanel.setHeading("选择简历");
		
		hp.add(leftPanel);
		hp.add(vp);
		hp.add(rightPanel);
		add(hp);
	}

	BaseTreeStore<TreeNodeModelData> leftTreeStore = null;

	private TreePanel<TreeNodeModelData> getTreePanel() {
		if (leftTreePanel == null) {
			initTreePanel();
		}
		return leftTreePanel;
	}

	private BaseTreeStore<TreeNodeModelData> getTreeStore() {
		if (leftTreeStore == null) {
			initTreeStore();
		}
		return leftTreeStore;
	}

	private BaseTreeStore<TreeNodeModelData> initTreeStore() {
		StoreInfo info = new StoreInfo();
		String sql2 = "select icode,depCode + requestNo as id,position as name,requestNo from hr_recruitmentRequest where requestNo is not null" +
				" union all select icode,id,name,null as requestNo from SystemInfo where id is not null order by id asc";
		info.setSql(sql2);
		info.setTreesortcolumn("id");
		info.setKeyname(new String[] { "icode" });
		info.setTablename("SystemInfo");
		leftTreeStore = new BaseTreeStore<TreeNodeModelData>(info, false);
		leftTreeStore.load();
		return leftTreeStore;
	}

	private void initTreePanel() {
		leftTreePanel = new TreePanel<TreeNodeModelData>(getTreeStore());
		Tools.setResize(leftTreePanel, 0.15, 0.93);
		leftTreePanel.setDisplayProperty("name");
		leftTreePanel.setCheckStyle(CheckCascade.PARENTS);
		leftTreePanel.setCheckNodes(CheckNodes.BOTH);
		leftTreePanel.getStyle().setLeafIcon(
				AbstractImagePrototype
						.create(BaseResource.INSTANCE.tree_file()));
		leftTreePanel.setCheckable(false);
		leftTreePanel.setAutoLoad(true);
		leftTreePanel.setCaching(true);

		leftTreePanel.addListener(Events.OnClick,
				new Listener<TreePanelEvent<TreeNodeModelData>>() {
					public void handleEvent(TreePanelEvent<TreeNodeModelData> be) {
						if (be.getItem() != null) {
							if (be.getItem().getChildCount() == 0 && be.getItem().get("requestNo") != null) {
								String id = be.getItem().get("icode").toString();
								leftTreeDepId = id;
								store.setValue("recruitmentId", leftTreeDepId);
							}
						}
					}
				});
	}

	BaseTreeStore<TreeNodeModelData> rightTreeStore = null;

	private TreePanel<TreeNodeModelData> getRightTreePanel() {
		if (rightTreePanel == null) {
			initRightTreePanel();
		}
		return rightTreePanel;
	}

	private BaseTreeStore<TreeNodeModelData> getRightTreeStore() {
		if (rightTreeStore == null) {
			initRightTreeStore();
		}
		return rightTreeStore;
	}

	private BaseTreeStore<TreeNodeModelData> initRightTreeStore() {
		StoreInfo info = new StoreInfo();
		String sql2 = "select icode,depCode + resumeNo as id,name,resumeNo from hr_resumeLibrary where resumeNo is not null" +
		" union all select icode,id,name,null as resumeNo from SystemInfo where id is not null order by id asc";
		info.setSql(sql2);
		info.setTreesortcolumn("id");
		info.setKeyname(new String[] { "icode" });
		info.setTablename("SystemInfo");
		rightTreeStore = new BaseTreeStore<TreeNodeModelData>(info, false);
		rightTreeStore.load();
		return rightTreeStore;
	}

	private void initRightTreePanel() {
		rightTreePanel = new TreePanel<TreeNodeModelData>(getRightTreeStore());
		Tools.setResize(rightTreePanel, 0.15, 0.93);
		rightTreePanel.setDisplayProperty("name");
		rightTreePanel.setCheckStyle(CheckCascade.PARENTS);
		rightTreePanel.setCheckNodes(CheckNodes.BOTH);
		rightTreePanel.getStyle().setLeafIcon(
				AbstractImagePrototype
						.create(BaseResource.INSTANCE.tree_file()));
		rightTreePanel.setCheckable(false);
		rightTreePanel.setAutoLoad(true);
		rightTreePanel.setCaching(true);

		rightTreePanel.addListener(Events.OnClick,
				new Listener<TreePanelEvent<TreeNodeModelData>>() {
					public void handleEvent(TreePanelEvent<TreeNodeModelData> be) {
						if (be.getItem() != null) {
							if (be.getItem().getChildCount() == 0) {
								String id = be.getItem().get("icode").toString();
								rightTreeDepId = id;
								store.setValue("resumeLibraryId", rightTreeDepId);
							}
						}
					}
				});
	}

	BaseStore<BusiObject> store = null;

	private BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	private BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("recruitmentId");
		type.getField("recruitmentId").setType(Integer.class);
		type.addField("resumeLibraryId");
		type.getField("resumeLibraryId").setType(Integer.class);
		type.addField("informationForm");
		type.getField("informationForm").setType(String.class);
		type.addField("requestDate");
		type.getField("requestDate").setType(Date.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_positionRequest");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_positionRequest where 1=2");
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	ModelType type = new ModelType();

	BaseGrid<BusiObject> grid = null;

	private BaseGrid<BusiObject> getGrid() {
		if (grid == null) {
			createGrid();
		}
		return grid;
	}

	/**
	 * 定义一个grid，用来显示手册的数据，并通过实例化一个SignleFileUpload 对象来显示一个上传对话框。
	 * 
	 * @return grid 一个BaseGrid对象
	 */
	private BaseGrid<BusiObject> createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C2 = new ColumnConfig("recruitmentId", "职位编号", 120);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("resumeLibraryId", "简历编号", 120);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("informationForm", "信息来源", 120);
		BaseComboBoxX box = new BaseComboBoxX("name", "dictionary", " where type = 'informationFrom'");
		C4.setEditor(box.getCellEditor());
		columns.add(C4);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm) {
			@SuppressWarnings("unchecked")
			public void addnewrecord() {
				if(leftTreeDepId == null) {
					super.addnewrecord();
				} else {
					HashMap hp = new HashMap();
					hp.put("recruitmentId", leftTreeDepId);
					hp.put("requestDate", new Date());
					grid.getStore().add(hp);
				}
			}
		};
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		Tools.setResize(grid, 0.70, 0.98);
		return grid;
	}
}
